import {Component, OnInit} from '@angular/core';
import {ProjectService} from "../service/project.service";
import {BarnProject} from "../entity/barn-project";
import {ActivatedRoute} from "@angular/router";
import {FormControl, FormGroup, Validators} from "@angular/forms";
import {NzMessageService} from "ng-zorro-antd/message";
import {ProductService} from "../../product/service/product.service";
import {BarnProduct} from "../../product/entity/barn-product";
import {DataConstant} from "../../../../../../constants/DataConstant";
import {ProjectProductService} from "../../service/project-product.service";
import {CloneProjectDto} from "../entity/clone-project-dto";

@Component({
  selector: 'app-project-usage',
  templateUrl: './project-usage.component.html',
  styleUrls: ['./project-usage.component.less']
})
export class ProjectUsageComponent implements OnInit {

  project: BarnProject
  loading = false
  remarkMaxLength = BarnProject.remarkMaxLength
  remarkForm = new FormGroup({
    remark: new FormControl("", Validators.maxLength(this.remarkMaxLength))
  })
  cloneModal = false


  products: BarnProduct[] = []
  productDialog = false
  configProducts: BarnProduct[] = []
  loadingProducts = false

  constructor(private projectService: ProjectService, private routeInfo: ActivatedRoute, private msg: NzMessageService,
              private productService: ProductService, private ppService: ProjectProductService) {
  }

  ngOnInit(): void {
    const pid = this.routeInfo.snapshot.queryParams["pid"];
    if (pid) {
      this.getById(pid)
      this.getConfigProducts(pid)
    }
    const param = new BarnProduct()
    param.enable = DataConstant.ENABLE_YES
    this.productService.query(param)
      .subscribe(value => {
        this.products = value.data
      });
  }

  getById(pid) {
    this.loading = true
    this.projectService.getById(pid).subscribe(value => {
      this.loading = false;
      this.project = value
      this.remarkForm.patchValue(value)
    });
  }

  saveRemark() {
    if (this.remarkForm.valid) {
      this.project.remark = this.remarkForm.get('remark').value
      this.projectService.save(this.project).subscribe(value => {
        this.msg.success("保存成功");
        this.loading = false
      }, () => this.loading = false);
    } else {
      this.remarkForm.controls[0].markAsDirty()
      this.remarkForm.controls[0].updateValueAndValidity()
    }
  }

  getConfigProducts(pid: string) {
    this.loadingProducts = true
    this.ppService.getProductByProject(pid)
      .subscribe(value => {
        this.configProducts = value;
        this.loadingProducts = false;
      }, () => this.loadingProducts = false)

  }

  addProductDialog() {
    this.productDialog = true;
  }

  computedProducts(): BarnProduct[] {
    console.log(this.configProducts)
    if (this.configProducts.length) {
      return this.products.filter(v => !this.configProducts.find(v2 => v2.pid === v.pid))
    } else {
      return this.products;
    }
  }

  dialogClose() {
    this.productDialog = false
    this.products.forEach(value => value.selected = false)
  }

  handleOk() {
    const selected = this.products.filter(value => value.selected).map(value => value.pid);
    this.ppService.projectAddProducts(this.project.pid, selected)
      .subscribe(() => {
        this.dialogClose()
        // 重新查询 configProducts
        this.getConfigProducts(this.project.pid)
      }, () => this.loading = false)
  }

  deleteProduct(productId) {
    this.loadingProducts = true
    this.ppService.delete(this.project.pid, productId).subscribe(() => {
      this.loadingProducts = false
      this.getConfigProducts(this.project.pid)
    }, () => this.loadingProducts = false)
  }

  doClone(project: BarnProject) {

    const param = new CloneProjectDto()
    param.fromProjectId = project.pid;
    param.toProjectId = this.project.pid
    this.projectService.clone(param).subscribe(() => {
      this.cloneModal = false
      this.msg.success("操作成功！")
      this.getById(this.project.pid)
      this.getConfigProducts(this.project.pid)
    })
  }
}
